home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / stringsearch / bmsource / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-10  |  2.8 KB  |  138 lines  |  [TEXT/MPS ]

  1. #include    <stdio.h>
  2. #include    "stats.h"
  3.  
  4. #define        NTIME        1000
  5.  
  6. struct stats stats;
  7. char buf[1001025];
  8. char wbuf[101000];
  9. long tm[NTIME+1];
  10. int nbuf;
  11. extern int hz;
  12. int option = -1;
  13.  
  14. cmp(a, b)
  15.     long *a, *b;
  16. {
  17.     if(*a < *b)
  18.         return(-1);
  19.     else
  20.         return((*a > *b)? 1:0);
  21. }
  22.  
  23. main(argc, argv)
  24.     char **argv;
  25. {
  26.     int c, cnt;
  27.     int npreproc = 1;
  28.     int nexec = 1;
  29.     long tp, te, t;
  30.     long *tmp = tm;
  31.     int nmatched, len;
  32.     long nf, nw;
  33.     long nstep, tstep;
  34.     double var, mean;
  35.     char *s, *ss, *sys = "?";
  36.     int percent = 0;
  37.     extern char *optarg;
  38.     extern int optind;
  39.  
  40.     while((c = getopt(argc, argv, "%e:p:s:o:")) != -1)
  41.         switch(c)
  42.         {
  43.         case 'p':    npreproc = atoi(optarg); break;
  44.         case 'e':    nexec = atoi(optarg); break;
  45.         case 's':    sys = optarg; break;
  46.         case 'o':    option = atoi(optarg); break;
  47.         case '%':    percent = 1; break;
  48.         default:    goto usage;
  49.         }
  50.     if((optind >= argc) || (optind+2 < argc)){
  51. usage:
  52.         fprintf(stderr, "usage: %s [-e nexec] [-p npreproc] [string] file\n", argv[0]);
  53.         exit(1);
  54.     }
  55.     if((c = open(argv[argc-1], 0)) < 0){
  56.         perror(argv[argc-1]);
  57.         exit(1);
  58.     }
  59.     nbuf = read(c, buf+1, sizeof buf-1025);
  60.     close(c);
  61.     if(optind+2 == argc){
  62.         te = strlen(argv[optind]);
  63.         strcpy(wbuf, argv[optind]);
  64.         wbuf[te+1] = 0;
  65.     } else {
  66.         s = wbuf;
  67.         while((c = getchar()) != EOF)
  68.             *s++ = (c=='\n')? 0:c;
  69.         *s = 0;
  70.     }
  71.     tp = te = 0;
  72.     nf = nw = 0;
  73.     var = 0;
  74. #ifdef    STATS
  75.     for(c = 0; c < NSTEP; c++)
  76.         stats.step[c] = 0;
  77.     stats.jump = 0;
  78.     stats.cmp = 0;
  79.     stats.slow = 0;
  80.     stats.extra = 0;
  81.     nmatched = 0;
  82. #endif
  83.     for(s = wbuf; *s;){
  84.         nw++;
  85.         /* convert # to \n so we can do multiword matches */
  86.         ss = s;
  87.         while(*s){
  88.             if(*s == '#')
  89.                 *s = '\n';
  90.             s++;
  91.         }
  92.         len = s-ss;
  93.         s++;
  94.         startclock();
  95.         for(c = 0; c < npreproc; c++)
  96.             prep(ss, len);
  97.         tp += stopclock();
  98.         startclock();
  99.         for(c = 0; c < nexec; c++){
  100.             if(cnt = exec(buf+1, nbuf))
  101.                 nmatched++;
  102.             nf += cnt;
  103.         }
  104.         te += (t = stopclock());
  105.         if(tmp < &tm[NTIME])
  106.             *tmp++ = t;
  107.         var += t*1.0*t;
  108.     }
  109. #ifdef    STATS
  110.     for(nstep = tstep = 0, c = 0; c < NSTEP; c++)
  111.         if(stats.step[c]){
  112. /*            printf("n(%d)=%d\n", c, stats.step[c]);/**/
  113.             nstep += stats.step[c];
  114.             tstep += stats.step[c]*c;
  115.         }
  116.     printf("sys=%s alg=%s match=%d ninput=%d step=%.2f cmp+jump=%.1f(%.1f%%)(%.1f+%.1f) slow=%.1f nmatched=%d extra=%.2f\n", sys, argv[0], nf, nbuf,
  117.         tstep/(nstep*(float)nexec),
  118.         1.*(stats.cmp+stats.jump)/nw, (stats.cmp+stats.jump)*100./(nbuf*nw),
  119.         1.*stats.cmp/nw, 1.0*stats.jump/nw, 1.0*stats.slow/nw, nmatched,
  120.         1.0*stats.extra/nw);
  121. #else
  122.     printf("sys=%s alg=%s nw=%d match=%d p=%.4fs(%d/%d) e=%.4fs(%d/%d)\n",
  123.         sys, argv[0], nw, nf/nexec,
  124.         (tp*1.0)/(hz*npreproc), tp, npreproc,
  125.         (te*1.0)/(hz*nexec), te, nexec);
  126.     if(percent){
  127.         mean = te/nw;
  128.         printf("%% %.8e", (var/nw-mean*mean)/(hz*hz*nexec));
  129.         qsort(tm, nw, sizeof(tm[0]), cmp);
  130.         tm[nw] = tm[nw-1];
  131.         for(c = 0; c <= 20; c++)
  132.             printf(" %.4e", (1.0*tm[(c*nw)/20])/(hz*nexec));
  133.         printf("\n");
  134.     }
  135. #endif
  136.     exit(0);
  137. }
  138.